/*
 *  Copyright (C) 2012 Компания ВИТ. web: http://diasled.ru Е-mail:  vit@diasled.ru
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ru.apertum.diasled.model.exam;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import ru.apertum.diasled.model.Examination;

/**
 *
 * @author Evgeniy Egorov, Aperum Projects
 */
public class RootNodeExam extends AExamNode {

    @Override
    public String toString() {
        return "Обследования во время посещения";
    }
    final private Set<Examination> nodes;
    final private HashMap<Integer, TypeShoesNode> shoesTypesNodeAll = new HashMap<>();
    final private LinkedList<TreeNode> shoesTypesNodeVisible = new LinkedList<>();

    @Override
    public LinkedList<TreeNode> getVisibleChildren() {
        return shoesTypesNodeVisible;
    }

    public RootNodeExam(Set<Examination> nodes) {
        this.nodes = nodes;
        createChildren();
    }

    private void createChildren() {
        for (String shoesName : Shoes.getInstance().getItems()) {
            final TypeShoesNode node = new TypeShoesNode(shoesName, this, nodes);
            shoesTypesNodeAll.put(Shoes.getInstance().getIdByStr(shoesName), node);
            for (Examination exam : nodes) {
                if (exam.getShoesType() == Shoes.getInstance().getIdByStr(shoesName)) {
                    shoesTypesNodeVisible.add(node);
                    break;
                }
            }
        }
    }

    @Override
    public TreeNode getParent() {
        return null;
    }

    public TreeNode addExam(Examination exam) {
        nodes.add(exam);
        shoesTypesNodeAll.clear();
        shoesTypesNodeVisible.clear();
        createChildren();
        return shoesTypesNodeAll.get(exam.getShoesType()).elemsNodeAll.get(exam.getInsideElement()).getExamNodeByExam(exam);
    }

    public TreeNode removeExam(Examination exam) {
        nodes.remove(exam);
        shoesTypesNodeAll.clear();
        shoesTypesNodeVisible.clear();
        createChildren();
        return shoesTypesNodeAll.get(exam.getShoesType()).elemsNodeAll.get(exam.getInsideElement());
    }
}
